package com.abkj.platform.tail.service.impl;

import com.abkj.platform.base.dao.warp.MapperWrap;
import com.abkj.platform.base.dao.warp.SQLType;
import com.abkj.platform.tail.service.DataService;
import com.abkj.platform.tail.socket.SocketUtil;
import com.abkj.platform.tail.util.JsonUtils;
import com.abkj.platform.util.PublicTool;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("dataService")
public class DataServiceImpl
  implements DataService
{

  @Autowired
  private MapperWrap mapperWrap;

  public void operateWithScanData(byte[] recvBuf, String code, int sid, String tablename, int leveltype, int levelnum)
  {
    String time = getDate(recvBuf);

    byte[] wendu = { recvBuf[20], recvBuf[21] };
    byte[] ybz = { recvBuf[22], recvBuf[23] };
    byte[] pyz = { recvBuf[24], recvBuf[25] };

    byte type = recvBuf[34];

    double num = Math.pow(10.0D, type >>> 4);

    float wenduval = PublicTool.bytesToShort(wendu) / 10;
    float ybzval = (float)(PublicTool.bytesToShort(ybz) / num);
    float pyzval = (float)(PublicTool.bytesToShort(pyz) / num);
    StringBuffer buff = new StringBuffer();
    if ((tablename.equals("level")) && (leveltype == 0)) {
      List numbase = this.mapperWrap.findBySql("select should from level where num=" + levelnum + " and type=1 ");
      if (numbase.size() > 0) {
        Map m = (Map)numbase.get(0);
        float baseshould = NumberUtils.toInt(String.valueOf(m.get("should")));
        float chage = baseshould - ybzval;
        buff.append("insert into ");
        buff.append(tablename);
        buff.append("data  (sid,createtime,should,offer,val,`change`,day) values (");
        buff.append(sid + ",");
        buff.append(time + ",");
        buff.append(ybzval + ",");
        buff.append(pyzval + ",");
        buff.append(wenduval + ",");
        buff.append(chage + ",");
        buff.append(time.substring(0, 8));
        buff.append(")");
      } else {
        buff.append("insert into ");
        buff.append(tablename);
        buff.append("data  (sid,createtime,should,offer,val,day) values (");
        buff.append(sid + ",");
        buff.append(time + ",");
        buff.append(ybzval + ",");
        buff.append(pyzval + ",");
        buff.append(wenduval + ",");
        buff.append(time.substring(0, 8));
        buff.append(")");
      }
    } else {
      buff.append("insert into ");
      buff.append(tablename);
      buff.append("data  (sid,createtime,should,offer,val,day) values (");
      buff.append(sid + ",");
      buff.append(time + ",");
      buff.append(ybzval + ",");
      buff.append(pyzval + ",");
      buff.append(wenduval + ",");
      buff.append(time.substring(0, 8));
      buff.append(")");
    }
    this.mapperWrap.excuteBySql(SQLType.UPDATE, buff.toString());
    buff.setLength(0);
    buff = null;
    if (!tablename.equals("rain")) {
      this.mapperWrap.excuteBySql(SQLType.UPDATE, "update " + tablename + " set val=" + wenduval + ", should=" + ybzval + ",offer=" + pyzval + " where id=" + sid);

      Map data = new HashMap();
      data.put("code", code);
      data.put("type", tablename);
      data.put("should", Float.valueOf(ybzval));
      data.put("offer", Float.valueOf(pyzval));
      SocketUtil.notifyGuest(JsonUtils.writeObjToString(data));
    }
  }

  public void rainData(byte[] recvBuf)
  {
    StringBuffer buff = new StringBuffer();
    buff.append(PublicTool.getHexString(recvBuf[38]));
    buff.append(PublicTool.getHexString(recvBuf[39]));
    buff.append(PublicTool.getHexString(recvBuf[40]));
    int day = Integer.parseInt("20" + buff.toString());
    buff.setLength(0);
    buff = null;
    byte[] v = { recvBuf[44], recvBuf[45] };
    int val = PublicTool.bytesToShort(v);
    String all = PublicTool.getHexString(recvBuf, 46, 48);
    List ms = this.mapperWrap.findBySql("select * from raindata where day = " + day);
    if (ms.size() > 0)
      this.mapperWrap.excuteBySql(SQLType.UPDATE, "update raindata set createtime=now() ,val=" + val + " ,allval='" + all + "' where day = " + day);
    else
      this.mapperWrap.excuteBySql(SQLType.UPDATE, "insert into raindata(day,val,createtime,allval) values(" + day + "," + val + ",now(),'" + all + "')");
  }

  private String getDate(byte[] recvBuf)
  {
    StringBuffer buff = new StringBuffer();
    buff.append(PublicTool.getHexString(recvBuf[14]));
    buff.append(PublicTool.getHexString(recvBuf[15]));
    buff.append(PublicTool.getHexString(recvBuf[16]));
    buff.append(PublicTool.getHexString(recvBuf[17]));
    buff.append(PublicTool.getHexString(recvBuf[18]));
    buff.append(PublicTool.getHexString(recvBuf[19]));
    String time = buff.toString();
    buff.setLength(0);
    return "20" + time;
  }
}